package com.ljx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljx.JwtUtils;
import com.ljx.R;
import com.ljx.pojo.TOrder;
import com.ljx.service.TOrderService;
import com.ljx.service.impl.TOrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import sun.nio.cs.US_ASCII;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 李加喜
 * @since 2020-10-02
 */
@RestController
@RequestMapping("/eduorder/order")
@CrossOrigin
public class TOrderController {
    @Autowired
    private TOrderServiceImpl tOrderService;
    //生成订单
    @PostMapping("/addOrder/{courseId}")
    public R addOrder(@PathVariable String courseId, HttpServletRequest request){
        //System.out.println("55555555");
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        //创建订单,生成订单号  后面需要根据这个订单号进行支付
        String OrderNo =tOrderService.saveOrder(courseId, userId);
        //System.out.println(OrderNo+"------>");
        return R.ok().data("OrderNo",OrderNo);
    }
    //点击立即购买后会调用saveOrder将信息保存在数据库里面
    //所以需要调用该方法通过订单号查询订单的详细信息
    @PostMapping("getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId) {
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        //根据订单号查询订单的信息
        wrapper.eq("order_no",orderId);
        TOrder order = tOrderService.getOne(wrapper);
        return R.ok().data("item",order);
    }
    //判断课程的支付状态  根据课程的ID与用户的Id查询课程的支付状态
    //如果status==1?
    @GetMapping("/isBuyCourse/{courseId}/{memberId}")
    public Boolean isBuyCourse(@PathVariable String courseId, @PathVariable String memberId){
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        wrapper.eq("status",1);
        int count = tOrderService.count(wrapper);
        if (count>0){
            return true;
        }else
            return false;
    }

//建立授权模块  并在授权模块里面添加依赖jjwt和spring-cloud-security
}

